package eclipse.util.maptools;
import java.awt.*;
import javax.swing.*;
import com.sun.opengl.util.texture.Texture;

import eclipse.client.Database;

import javax.media.opengl.jgloo.jglfx.*;

import eclipse.lib.map.*;
import eclipse.util.maptools.properties.*;
/**
 * The MapEditor exists as a stand-alone application that allows the editing of maps. 
 * Also, the MapEditor class has a "default" setting, the static property: "editor"/
 * "editor" can be used outside the application for associating the map editor with other applications.
 * For the Map Editor to work, the environment must be set up exactly how it is for the client 
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
//TODO: MULTISELECT
//TODO: Save tile colors
public final class MapEditorApplication 
{
	/** Palate represents the tile selector */
	public static Palette	tilePalette;
	/** MapRenderer draws the map */
	public static Renderer mapRenderer;	
	/** The General Properties editor object */
	public static MapGeneralProperties generalProperties;
	/** Lighting properties editor Object */
	public static MapLightingProperties lightingProperties;
	/** Linking properties editor Object */
	public static MapLinkingProperties linkingProperties;
	/** environment is the window object that everything is drawn within */
	public static JFrame 	environment;	
	/** X-position of the selected tile */
	public static int selectX = 0;
	/** Y-position of the selected tile */
	public static int selectY = 0;
	/** Tile Sheet of the selected tile */
	public static int selectSheet = 0;
	/** Light count */
	public static final int LIGHTCOUNT = 1;
	/** The layer that is selected for editing*/
	public static Layer visLayer;	
	/** The map that is being edited */
	public static Map map;
	/**
	 * true = all layers displayed
	 * false = only single layer displayed
	 */
	public static boolean displayAll = false;
	/**
	 * true = lights will be used
	 * false = lights will not be used
	 */
	public static boolean useLights = false;
	/** Color that is used for the tiles */
	public static Color tColor = Color.WHITE;
	/** Attribute that is currently selected */
	public static int selAttribute = 0;
	/**
	 *  true will switch to attribute editor
	 *  false will switch back */
	public static boolean drawAttr = false;
	/** draws light */
	public static JGLFilter lightSun;
	/**the number of tilesheets that exist within the environment */
	public static int TILESHEETS;
	/**workspace*/
	public static String workspace;	
	// Application entry point
	public static final void main(String[] args) 
	{			
		workspace = args[0];
		TILESHEETS = Integer.parseInt(args[1]);
		
		//Allocate enough space for the tilesheets
		Database.tTileSheet = new Texture[TILESHEETS];			
		
		//build the desktop pane object
		final JDesktopPane desktop 	= new JDesktopPane();
		//build the renderer object
		mapRenderer 			= new Renderer();
		mapRenderer.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		//build the Palette object
		tilePalette	 			= new Palette();
		//build the general properties object
		generalProperties		= new MapGeneralProperties();
		//build the lighting properties object
		lightingProperties		= new MapLightingProperties();
		//build the linking properties object
		linkingProperties		= new MapLinkingProperties();
		//build the window for the application
		environment 			= new JFrame("Solar Eclipse Map Editor");
		
		//overwrite the window with the desktop pane
		environment.setContentPane(desktop);
		//add the window 'mapRenderer'		
		desktop.add(mapRenderer);
		//add the window 'tilePalette
		desktop.add(tilePalette);
		//add the mapGeneralProperties window
		desktop.add(generalProperties);
		//add the mapLightingProperties window
		desktop.add(lightingProperties);
		//add the mapLinkingProperties window
		desktop.add(linkingProperties);
		//set the size of the frame to 800x600
		environment.setSize(800, 600);
		//Create the menubar for the window
		environment.setJMenuBar(new MapEditorMenuBar());
		//show the windows
		environment.setVisible(true);
		//show the virtual desktop
		desktop.setVisible(true);
		//show the map renderer window within the virtual desktop
		mapRenderer.setVisible(true);
		//show the tile palette window within the virutal desktop
		tilePalette.setVisible(true);
		
		//make it so that the application ends when the close button is clicked 
		environment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	/**
	 * Resets all of the components by overwritting them.
	 */
	public final static void reset(int layers)
	{
		//reset the respawn map to 0
		generalProperties.txtMap.setText("0");
		//reset the map name to nothing
		generalProperties.txtMapName.setText("");
		//reset the music text to nothing
		generalProperties.txtMusic.setText("");
		//reset the respawn x to nothing
		generalProperties.txtX.setText("0");
		//reset the respawn y to nothing
		generalProperties.txtY.setText("0");
		//reset the respawn z to nothing
		generalProperties.txtZ.setText("0");
		
		//create a new object to draw light on
		lightSun = new JGLFilter();
		
		//disable all lights
		lightingProperties.chkEnabled.setSelected(false);
		//reset the selected light
		lightingProperties.cmbLight.setSelectedIndex(0);
		//reset the color to white
		lightingProperties.colorChooser.setColor(255, 255, 255);
		
		//reset the linked map east
		linkingProperties.txtEast.setText("0");
		//reset the linked map north
		linkingProperties.txtNorth.setText("0");
		//reset the linked map south
		linkingProperties.txtSouth.setText("0");
		//reset the linked map west
		linkingProperties.txtWest.setText("0");
		
		//build a new map		
		map = new Map("", layers);
	}	
}